﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RecommenderSystem.misc;

namespace RecommenderSystem.algorithms.memoryBased
{
    public class NNPearson:Pearson
    {
        private Dictionary<string, IEnumerable<User>> userNearestNeighboors;

        public NNPearson(Dictionary<string, User> users, ICollection<IDataRecord> dataset) : base(users, dataset) 
        {
            userNearestNeighboors = new Dictionary<string, IEnumerable<User>>();
        }

        public NNPearson(Dictionary<string, User> users, ICollection<IDataRecord> dataset, Dictionary<string, Cache> cache)
            : base(users, dataset, cache)
        {
            userNearestNeighboors = new Dictionary<string, IEnumerable<User>>();
        }

        public override double predictRating(string userID, string itemID)
        {
            double similarity, answer = 0d;
            if (!userNearestNeighboors.ContainsKey(userID))
            {
                userNearestNeighboors.Add(userID, getKNearestNeighboors(userID, Config.K_NEAREST_NEIGHBOORS));
            }

            foreach (User envUser in userNearestNeighboors[userID])
            {
                if (envUser.getItemRate(itemID) != -1)
                {
                    similarity = base.calculateSimilarity(_users[userID], envUser);
                    answer += similarity;
                }
            }

            return answer;
        }
    }
}
